/**
 * Copyright (c) 2009 Teoti Graphix, LLC See:
 *     http://tkarchitecture.teotigraphix.org
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 * @author Michael Schmalle mschmalle@teotigraphix.com
 */
package org.example.ui.behavior
{

import flash.events.Event;
import flash.events.IEventDispatcher;

import org.example.ui.component.IButton;
import org.teotigraphix.tka.patterns.behavior.SparkComponentBehavior;

import spark.components.supportClasses.TextBase;

//--------------------------------------
// ComponentHandlers
//--------------------------------------

[ComponentHandler(event="contentChanged", handler="contentHandler")]

/**
 * An abstracted Label display behavior for the <code>IButton</code> interface.
 * 
 * <p>Strive for loosely coupled references in these types of behaviors; 
 * interfaces are your friend.</p>
 * 
 * @copyright Teoti Graphix, LLC
 * @author Michael Schmalle
 * @date 07-15-09
 */
public class ButtonLabelBehavior 
	   extends SparkComponentBehavior
	   implements ILabelBehavior
{
	//--------------------------------------------------------------------------
	//
	//  Public SkinPart :: Variables
	//
	//--------------------------------------------------------------------------
	
	[SkinPartAlias]
	
	/**
	 * The component that will display the text string.
	 */	
	public var labelDisplay:TextBase;
	
	//--------------------------------------------------------------------------
	//
	//  Public ComponentAlias :: Variables
	//
	//--------------------------------------------------------------------------
		
	[ComponentAlias]
	
	/**
	 * The loosely coupled button Component.
	 */	
	public var button:IButton;
	
	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function ButtonLabelBehavior()
	{
		super(); 
	}
	
	//--------------------------------------------------------------------------
	//
	//  Overridden Protected :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @inheritDoc
	 * @uses #commitLabelDisplay()
	 */
	override protected function partAdded(partName:String, instance:Object):void
	{
		if (instance == labelDisplay)
		{
			commitLabelDisplay();
		}
	}
	
	/**
	 * @private 
	 */
	override protected function addingBehavior(instance:IEventDispatcher):void
	{
		super.addingBehavior(instance);
		
		if (behaviorLayer &&
			!behaviorLayer.hasBehavior(TruncatedToolTipBehavior))
		{
			behaviorLayer.registerBehavior(
				TruncatedToolTipBehavior, new TruncatedToolTipBehavior());
		}
	}
	
	/**
	 * @private 
	 */
	override protected function removingBehavior(instance:IEventDispatcher):void
	{
		if (behaviorLayer &&
			behaviorLayer.hasBehavior(TruncatedToolTipBehavior))
		{
			behaviorLayer.removeBehavior(TruncatedToolTipBehavior);
		}
		
		super.removingBehavior(instance);	
	}	
	
	//--------------------------------------------------------------------------
	//
	//  Protected :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Commits the <code>IButton.content</code> data to the <code>labelDisplay</code>
	 * instance.
	 * @uses org.example.components.IButton#content
	 */
	protected function commitLabelDisplay():void
	{
		if (button.content === undefined)
			return;
		
		labelDisplay.text = button.content;
	}
	
	//--------------------------------------------------------------------------
	//
	//  component_handler :: Handlers
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	//  Component
	//----------------------------------
	
	/**
	 * Handles the <code>IButton.icon</code> assignment change from the Component.
	 * @param event An Event.
	 * @uses #commitLabelDisplay()
	 */
	component_handler function contentHandler(event:Event):void
	{
		commitLabelDisplay();
	}
}
}